package com.作业.Day18;

public class Homework4 {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyThread(),"桃跑跑");
        Thread thread1 = new Thread(new MyThread(),"张票票");
        Thread thread2 = new Thread(new MyThread(),"黄牛党");
        thread2.start();
        thread1.start();
        thread.start();
    }
}
class MyThread implements Runnable {
    static int ticket = 10;
    static Object object = new Object();

    @Override
    public void run() {


            while (ticket > 0) {
                synchronized (object){
                    if(ticket<=0) break;
                if (Thread.currentThread().getName().equals("黄牛党")) {
                    System.out.println(Thread.currentThread().getName() + "抢到第" + (11 - ticket) + "张票剩余" + --ticket + "张票！");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                } else{
                    System.out.println(Thread.currentThread().getName() + "抢到第" + (11 - ticket) + "张票剩余" + --ticket + "张票！");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                    object.notify();
            }
        }
    }}

